<?php
/**
 * 计算每个用户，每个月可用积分
 * @author yiwan
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\model\PointMonth as mPointMonth;
use Medoo\Medoo;

$t1 = microtime(true);

$startDate = '201805';
$endDate = '201903';
$tabIndex = 0;
if (isset($argv[1])) {
    $tabIndex = $argv[1];
}

$tableNum = mPointMonth::_SUB_TABLE_NUM;
if ($tabIndex >= $tableNum) {
    var_dump('不存在tabindex' . $tabIndex);
    die;
} else {
    var_dump(mPointMonth::_TABLE_NAME . $tabIndex . '表开始执行');
    $tabStar = microtime(true);
    mPointMonth::enforceSubTableNameSuffix($tabIndex);
    $uidArr = mPointMonth::select([
        'uid' => Medoo::raw('DISTINCT(uid)'),
    ], ['deduct_point[>]' => 0]);
    foreach ($uidArr as $row) {
        $uid = $row['uid'];
        $where = [
            'AND' => [
                'uid' => $uid,
                'month[>=]' => $startDate,
                'month[<=]' => $endDate
            ],
            'ORDER' => [
                'month' => 'ASC'
            ]
        ];
        $list = mPointMonth::select('*', $where);
        if ($list) {
            var_dump('开始执行uid:' . $uid);
            iniRemainPoint($list);
        }
    }
    $tabEnd = microtime(true);
    $time = $tabEnd - $tabStar;
    var_dump(mPointMonth::_TABLE_NAME . $tabIndex . '表执行完毕。执行时间：' . number_format($time, 2) . 's ');
}

# test_data start>>>>>

//$list = [//all 53
//    ['id' => 993644, 'uid' => 4133139, 'month' => 201805, 'award_point' => 23, 'deduct_point' => 0],//0
//    ['id' => 1563331, 'uid' => 4133139, 'month' => 201810, 'award_point' => 3, 'deduct_point' => 0],//0
//    ['id' => 1634198, 'uid' => 4133139, 'month' => 201811, 'award_point' => 6, 'deduct_point' => 0],//0
//    ['id' => 1697229, 'uid' => 4133139, 'month' => 201812, 'award_point' => 12, 'deduct_point' => 0],//5
//    ['id' => 1802193, 'uid' => 4133139, 'month' => 201901, 'award_point' => 12, 'deduct_point' => 0],//12
//    ['id' => 2022998, 'uid' => 4133139, 'month' => 201902, 'award_point' => 18, 'deduct_point' => 33],//18
//    ['id' => 2059189, 'uid' => 4133139, 'month' => 201903, 'award_point' => 18, 'deduct_point' => 6],//18
//];
//
//iniRemainPoint($list);

# test_data end<<<<<

function iniRemainPoint($list)
{
    $updateMonthList = [];
    foreach ($list as $key => $row) {
        $debuctPoint = $row['deduct_point'];
        if ($debuctPoint > 0) {
            setRemainPoint($updateMonthList, $list, 0, $debuctPoint);
        } else {
            $updateMonthList[$row['id']] = $row['award_point'];
        }
    }
    foreach ($updateMonthList as $id => $currentPoint) {
        mPointMonth::updateData(['remain_point' => $currentPoint], ['id' => $id]);
    }
}

function setRemainPoint(&$updateMonthList, &$list, $key, $point)
{
    $info = $list[$key];
    if (!isset($info['current_point'])) {
        $info['current_point'] = $info['award_point'];
    }
    if ($info['current_point']) {
        $remainPoint = $info['current_point'] - $point;
        //var_dump($key . ':' . $remainPoint);
        if ($remainPoint < 0) {
            $updateMonthList[$list[$key]['id']] = 0;
            $list[$key]['current_point'] = 0;
            if (isset($list[$key + 1])) {
                setRemainPoint($updateMonthList, $list, $key + 1, abs($remainPoint));
            }
        } else {
            $updateMonthList[$list[$key]['id']] = $remainPoint;
            $list[$key]['current_point'] = $remainPoint;
            if (isset($list[$key + 1])) {
                setRemainPoint($updateMonthList, $list, $key + 1, 0);
            }
        }
    } else {
        if (isset($list[$key + 1])) {
            setRemainPoint($updateMonthList, $list, $key + 1, $point);
        }
    }
}


